<?php
namespace kykkk;
// +-----------------------------------------------------------------------
// | WWW.KYKKK.COM 科友CMS_网站管理系统 - 将军出品
// +-----------------------------------------------------------------------
// | Copyright (c) 2014 http://WWW.KYKKK.COM All rights reserved.
// +-----------------------------------------------------------------------
// | Author: 将军 <33736535@qq.com>.<http://www.kykkk.com>
// +-----------------------------------------------------------------------
class Nav {
    //返回顶级
    static function getTop($arr,$parameter){
        $return = [];
        foreach($arr as $v){
            if($v['id']==$parameter){
                 if($v['pid']!=0){
                     $v=self::getTop($arr,$v['pid']);
                 }
                 $return=$v;
              }
        }
        return $return;
    }
    //返回父级
    static function getParent($arr,$parameter){
        foreach($arr as $v){
            if($v['id']==$parameter) return $v;
        }
    }
    //返回自己
    static function getSelf($arr,$parameter){
        if(is_numeric($parameter)){
            foreach($arr as $v){
                if($v['id']==$parameter) return $v;
            }
        }else{
            foreach($arr as $v){
                if($v['url']==$parameter) return $v;
            }
        }
    }

    //返回子级
    static function getSon($arr,$parameter){
        $return=array();
        foreach($arr as $v){
            if($v['pid']==$parameter){
                $return[]=$v;
                $return=array_merge($return,self::getSon($arr,$v['id']));
            }
        }
        return $return;
    }
    //返回等级列表
    static function getLevel($nav,$pid=0,$level=0,$html='&nbsp;'){
         $arr=[];
         foreach($nav as $v){
            if($v['pid']==$pid){
                $v['level']=$level;
                $v['html']=str_repeat($html,$level);
                $arr[]=$v;
                $arr=array_merge($arr,self::getLevel($nav,$v['id'],$level+1));
            }
         }
          return $arr;
    }

   

    //返回多维数组后台使用
    static function getMulti($arr,$pid=0){
        $return = [];
        foreach($arr as $v){
            if($v['pid'] == $pid){
                $v['son']=self::getMulti($arr,$v['id']);
                $return[]=$v;
            }
        }
        return $return;
    }

    //返回多维数组 - 前台使用
    static function getNavList($arr,$pid=0){
        $return = [];
        foreach($arr as $v){
            $v['url'] = 'category_'.$v['id'].'.html';
            if($v['pid'] == $pid){
                $v['son']=self::getNavList($arr,$v['id']);
                $return[]=$v;
            }
        }
       
        return $return;
    }


    //返回当前位置
    static function getPosition($cate,$id){
    	$arr=array();
    		foreach($cate as $v){
    			if($v['id'] == $id){
    				$arr[]=$v;
    				$arr=array_merge(self::getPosition($cate,$v['pid']),$arr);
    			}
    		}
    	return $arr;
    }
    //获取子级ID字符串
    static function getAllId($arr,$navid){
      
        $allid = [];
        foreach(self::getSon($arr,$navid) as $v){
            $allid[] = $v['id'];
        }
        $allid[] = $navid;
        $allid = implode(',', $allid);
        return $allid;
    }

    static function getRecommend($nav,$id){
        $arr = [];
        $son = self::getSon($nav,$id);

        foreach($son as $v){
            if($v['recommend'] ==1){
                $arr[] =$v;
            } 
        }
       return $arr;
    }

    static function getHot($nav,$id){
        $arr = [];
        $son = self::getSon($nav,$id);

        foreach($son as $v){
            if($v['hotnav'] ==1){
                $arr[] =$v;
            } 
        }
       return $arr;
    }

    static function get_cal($arr,$id){
        foreach($arr as $v){
            if($v['id'] == $id) return $v;
        }
    }




}